package com.ruoyi.web.controller.datamanagement;

import java.util.*;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.util.PageUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.anan.util.ZhihesjUtil;
import com.ruoyi.api.service.SelectService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.datamanagement.domain.SysDataSearch;
import com.ruoyi.datamanagement.service.ISysDataSearchService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 查询管理Controller
 *
 * @author anan
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/datamanagement/dataSearch")
public class SysDataSearchController extends BaseController
{
    @Autowired
    private ISysDataSearchService dataSearchService;

    @Autowired
    private SelectService selectService;

    /**
     * 查询查询管理列表
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDataSearch dataSearch)
    {
		QueryWrapper<SysDataSearch> wrapper = toSysDataSearch(dataSearch);
		PagePlus<SysDataSearch, SysDataSearch> sysDataSearchSysDataSearchPagePlus = dataSearchService.pageVo(PageUtils.buildPagePlus(), wrapper, SysDataSearch.class);
		return PageUtils.buildDataInfo(sysDataSearchSysDataSearchPagePlus);
    }

    /**
     * 查询查询管理列表根据ancestors
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:list')")
    @GetMapping("/getListByAncestors")
    public TableDataInfo getListByAncestors(SysDataSearch dataSearch)
    {
        List<SysDataSearch> list = dataSearchService.getListByAncestors(dataSearch);
		return PageUtils.buildDataInfo(list);
    }

    /**
     * 导出查询管理列表
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:export')")
    @Log(title = "查询管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDataSearch sysDataSearch)
    {
        List<SysDataSearch> list = dataSearchService.selectSysDataSearchList(sysDataSearch);
        ExcelUtil<SysDataSearch> util = new ExcelUtil<SysDataSearch>(SysDataSearch.class);
        return util.exportExcel(list, "search");
    }

    /**
     * 获取查询管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dataSearchService.getById(id));
    }

    /**
     * 新增查询管理
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:add')")
    @Log(title = "查询管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDataSearch dataSearch)
    {
    	if (!dataSearch.getSearchName().isEmpty()) {
			dataSearch.setSearchNameEn(PinyinUtil.getFirstLetter(dataSearch.getSearchName(), ""));
		}
        return toAjax(dataSearchService.save(dataSearch));
    }

    /**
     * 修改查询管理
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:edit')")
    @Log(title = "查询管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDataSearch dataSearch)
    {
		if (!dataSearch.getSearchName().isEmpty()) {
			dataSearch.setSearchNameEn(PinyinUtil.getFirstLetter(dataSearch.getSearchName(), ""));
		}
        return toAjax(dataSearchService.updateById(dataSearch));
    }

    /**
     * 删除查询管理
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:dataSearch:remove')")
    @Log(title = "查询管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dataSearchId}")
    public AjaxResult remove(@PathVariable Long dataSearchId)
    {
        return toAjax(dataSearchService.removeById(dataSearchId));
    }

    /**
     * 查询管理
     * @param map
     * @return
     */
    @GetMapping("/selectMethod1")
    public AjaxResult selectMethod(@RequestParam Map map){
        String method = (String) map.get("method");
        SysDataSearch dataSearch = new SysDataSearch();
        List list1 = new ArrayList();
        if (method!=null && !method.isEmpty()) {
            /** 获取查询语句 */
            dataSearch = selectService.getDataSourceByMethod(method);
            /** 根据查询语句获取结果 */
            List list = selectService.getData(dataSearch,map);
            /** 将后面获取的list转换成驼峰list */
            list1 = ZhihesjUtil.getConResults(list);
        }
        return AjaxResult.success(list1);
    }

	public QueryWrapper<SysDataSearch> toSysDataSearch(SysDataSearch dataSearch)
	{
		QueryWrapper<SysDataSearch> wrapper = new QueryWrapper<>();
		if (dataSearch.getDataSourceId()!=null){
			wrapper.eq("data_source_id", dataSearch.getDataSourceId());
		}
		if (dataSearch.getDataSearchId()!=null){
			wrapper.eq("data_search_id", dataSearch.getDataSearchId());
		}
		if (dataSearch.getSearchName()!=null){
			wrapper.like("search_name", dataSearch.getSearchName());
		}
		if (dataSearch.getSearchNameEn()!=null){
			wrapper.like("search_name_en", dataSearch.getSearchNameEn());
		}
		if (dataSearch.getSearchTreeId()!=null){
			wrapper.eq("search_tree_id", dataSearch.getSearchTreeId());
		}
		return wrapper;
	}
}
